package org.dromara.hotel.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.hotel.domain.bo.TerPriceCalendarBo;
import org.dromara.hotel.domain.vo.TerPayVo;
import org.dromara.tearoom.api.domain.vo.RemoteCoupVo;
import org.dromara.tearoom.api.domain.vo.RemoteHtlOrderVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.hotel.domain.vo.TerHotelOrdersVo;
import org.dromara.hotel.domain.bo.TerHotelOrdersBo;
import org.dromara.hotel.service.ITerHotelOrdersService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 酒店订单
 * 前端访问路由地址为:/hotel/hotelOrders
 *
 * @author xch
 * @date 2025-06-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hotelOrders")
public class TerHotelOrdersController extends BaseController {

    private final ITerHotelOrdersService terHotelOrdersService;

    /**
     * 查询酒店订单列表
     */
    @SaCheckPermission("hotel:hotelOrders:list")
    @GetMapping("/list")
    public TableDataInfo<TerHotelOrdersVo> list(TerHotelOrdersBo bo, PageQuery pageQuery) {
        return terHotelOrdersService.queryPageList(bo, pageQuery);
    }

    @GetMapping("/wechat/list")
    public TableDataInfo<TerHotelOrdersVo> queryOrder(TerHotelOrdersBo bo, PageQuery pageQuery) {
        return terHotelOrdersService.queryHotelPage(bo, pageQuery);
    }
    @GetMapping("/queryCoup")
    public R<List<RemoteCoupVo>> queryCoup(TerHotelOrdersBo bo) {
        return R.ok(terHotelOrdersService.remoteCoupVoList(bo));
    }
    @PostMapping("/wechat/refund")
    public R<Void> refund(@Validated(EditGroup.class) @RequestBody TerHotelOrdersBo bo) {
        return toAjax(terHotelOrdersService.refund(bo));
    }

    /**
     * 导出酒店订单列表
     */
    @SaCheckPermission("hotel:hotelOrders:export")
    @Log(title = "酒店订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TerHotelOrdersBo bo, HttpServletResponse response) {
        List<TerHotelOrdersVo> list = terHotelOrdersService.queryList(bo);
        ExcelUtil.exportExcel(list, "酒店订单", TerHotelOrdersVo.class, response);
    }

    /**
     * 获取酒店订单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("hotel:hotelOrders:query")
    @GetMapping("/{id}")
    public R<TerHotelOrdersVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        return R.ok(terHotelOrdersService.queryById(id));
    }

    /**
     * 新增酒店订单
     */
    @SaCheckPermission("hotel:hotelOrders:add")
    @Log(title = "酒店订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TerHotelOrdersBo bo) {
        return toAjax(terHotelOrdersService.insertByBo(bo));
    }
    /**
     * 修改酒店订单
     */
    @SaCheckPermission("hotel:hotelOrders:edit")
    @Log(title = "酒店订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TerHotelOrdersBo bo) {
        return toAjax(terHotelOrdersService.updateByBo(bo));
    }
    @Log(title = "酒店订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/wechat/createOrder")
    public TerPayVo createOrder(@Validated(AddGroup.class) @RequestBody
                                   TerHotelOrdersBo bo) {
        return terHotelOrdersService.createOrder(bo);
    }
    /**
     * 删除酒店订单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("hotel:hotelOrders:remove")
    @Log(title = "酒店订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") String[] ids) {
        return toAjax(terHotelOrdersService.deleteWithValidByIds(List.of(ids), true));
    }
}
